/***
This do-file plots two binscatters on changes in consumer spending by workplace
rent at the ZIP code level, one for the change from Jan-Apr 2020 and one for the
change from Jan-Oct 2020.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Spending"

* Create required subfolders
cap mkdir "${root}/results/Spending"

*-------------------------------------------------------------------------------
* Load data
*-------------------------------------------------------------------------------

project, uses("${root}/results/Datasets to release/Low-Income Spending vs Workplace Rent Binscatter Data.csv")
import delimited "${root}/results/Datasets to release/Low-Income Spending vs Workplace Rent Binscatter Data.csv", clear

*-------------------------------------------------------------------------------
* Binscatter for April 2020
*-------------------------------------------------------------------------------

sum slope if year == 2020 & month == 4
local slope = `r(mean)'
local display_slope: display %4.2f `slope'

sum se if year == 2020 & month == 4
local se = `r(mean)'
local display_se: display %4.2f `se'

tw (scatter spend_change_pp med_2br_workplace_rent_zip if year == 2020 & month == 4) ///
	(lfit spend_change_pp med_2br_workplace_rent_zip if year == 2020 & month == 4) ///
	, ///
	xtitle("Average Two Bedroom Monthly Rent in 2014-2018 in Workplace ZIP ($)") ///
	xscale(range(580 1830)) ///
	xlabel(600 "$600" 800 "$800" 1000 "$1,000" 1200 "$1,200" 1400 "$1,400" 1600 "$1,600" 1800 "$1,800") ///
	ytitle("Change in Consumer Spending (%)" "from January to April 2020") ///
	ylabel(-20 "-20%" -25 "-25%" -30 "-30%" -35 "-35%" -40 "-40%", nogrid) ///
	text(-40 580 "Slope = `display_slope'/$1000 (s.e. = `display_se')", place(ne) just(left) size(medlarge) color(gs8)) ///
	legend(off)

oi_graph_export "${root}/results/Spending/Change in Consumer Spending vs Workplace Rent - April 2020 - replication", type(${fig_type})

* Black and white version for QJE
tw (scatter spend_change_pp med_2br_workplace_rent_zip if year == 2020 & month == 4, mcolor(gs0)) ///
	(lfit spend_change_pp med_2br_workplace_rent_zip if year == 2020 & month == 4, lcolor(gs8)) ///
	, ///
	xtitle("Average Two Bedroom Monthly Rent in 2014-2018 in Workplace ZIP ($)") ///
	xscale(range(580 1830)) ///
	xlabel(600 "$600" 800 "$800" 1000 "$1,000" 1200 "$1,200" 1400 "$1,400" 1600 "$1,600" 1800 "$1,800") ///
	ytitle("Change in Consumer Spending (%)" "from January to April 2020") ///
	ylabel(-20 "-20%" -25 "-25%" -30 "-30%" -35 "-35%" -40 "-40%", nogrid) ///
	text(-40 580 "Slope = `display_slope'/$1000 (s.e. = `display_se')", place(ne) just(left) size(medlarge) color(gs8)) ///
	legend(off)

graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_9b.svg", replace
project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_9b.svg")

*-------------------------------------------------------------------------------
* Binscatter for October 2020
*-------------------------------------------------------------------------------

sum slope if year == 2020 & month == 10
local slope = `r(mean)'
local display_slope: display %4.2f `slope'

sum se if year == 2020 & month == 10
local se = `r(mean)'
local display_se: display %4.2f `se'

tw (scatter spend_change_pp med_2br_workplace_rent_zip if year == 2020 & month == 10) ///
	(lfit spend_change_pp med_2br_workplace_rent_zip if year == 2020 & month == 10) ///
	, ///
	xtitle("Average Two Bedroom Monthly Rent in 2014-2018 in Workplace ZIP ($)") ///
	xscale(range(580 1830)) ///
	xlabel(600 "$600" 800 "$800" 1000 "$1,000" 1200 "$1,200" 1400 "$1,400" 1600 "$1,600" 1800 "$1,800") ///
	ytitle("Change in Consumer Spending (%)" "from January to October 2020") ///
	ylabel(-5 "-5%" 0 "0%" 5 "+5%" 10 "+10%" 15 "+15%" , nogrid) ///
	text(-5 580 "Slope = `display_slope'/$1000 (s.e. = `display_se')", place(ne) just(left) size(medlarge) color(gs8)) ///
	legend(off)

oi_graph_export "${root}/results/Spending/Change in Consumer Spending vs Workplace Rent - October 2020 - replication", type(${fig_type})

* Black and white version for QJE
tw (scatter spend_change_pp med_2br_workplace_rent_zip if year == 2020 & month == 10, mcolor(gs0)) ///
	(lfit spend_change_pp med_2br_workplace_rent_zip if year == 2020 & month == 10, lcolor(gs8)) ///
	, ///
	xtitle("Average Two Bedroom Monthly Rent in 2014-2018 in Workplace ZIP ($)") ///
	xscale(range(580 1830)) ///
	xlabel(600 "$600" 800 "$800" 1000 "$1,000" 1200 "$1,200" 1400 "$1,400" 1600 "$1,600" 1800 "$1,800") ///
	ytitle("Change in Consumer Spending (%)" "from January to October 2020") ///
	ylabel(-5 "-5%" 0 "0%" 5 "+5%" 10 "+10%" 15 "+15%" , nogrid) ///
	text(-5 580 "Slope = `display_slope'/$1000 (s.e. = `display_se')", place(ne) just(left) size(medlarge) color(gs8)) ///
	legend(off)

graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_9c.svg", replace
project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_9c.svg")

*-------------------------------------------------------------------------------
* Export scalars for paper - change in low-income consumer spending vs. workplace rent in
* low-rent vs. high-rent places
*-------------------------------------------------------------------------------

* Lowest rent quartile
sum spend_change_pp if inrange(rent_ventile, 1, 5) & year == 2020 & month == 4
local apr_spend_workrent_q1: di %2.0f abs(r(mean))

* Highest rent quartile
sum spend_change_pp if inrange(rent_ventile, 16, 20) & year == 2020 & month == 4
local apr_spend_workrent_q4: di %2.0f abs(r(mean))

cap erase "${root}/results/paper numbers/`category'/Spending Changes by Workplace Rent.yaml"

yamlout using "${root}/results/paper numbers/`category'/Spending Changes by Workplace Rent.yaml", ///
	key("apr_spend_workrent_q1") ///
	comment("Change in low-income consumer spending in low workplace rent areas, Apr 2020") ///
	value(`apr_spend_workrent_q1') fmt(%9.0f)

yamlout using "${root}/results/paper numbers/`category'/Spending Changes by Workplace Rent.yaml", ///
	key("apr_spend_workrent_q4") ///
	comment("Change in low-income consumer spending in high workplace rent areas, Apr 2020") ///
	value(`apr_spend_workrent_q4') fmt(%9.0f)

project, creates("${root}/results/paper numbers/`category'/Spending Changes by Workplace Rent.yaml")
